from pylons import config
from sqlalchemy import Column, MetaData, Table, types
from sqlalchemy.orm import mapper
from sqlalchemy.orm import scoped_session, sessionmaker

Session = scoped_session(sessionmaker(autoflush=True, transactional=True,
                                      bind=config['pylons.g'].sa_engine))

engine = config['pylons.g'].sa_engine
meta = MetaData(engine)
model_classes = {}

for tn in engine.table_names():
    class_name = str(tn).capitalize()
    new_class = type( class_name , object.__bases__, dict (object.__dict__))
    new_table = Table( tn, meta, autoload=True)
    mapper(new_class, new_table)    

    model_classes[ class_name ] = new_class
